package cn.hrlly.tao2shou.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.utils.DatabaseUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;

public class SplashActivity extends Activity {

    private DatabaseUtil databaseUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        databaseUtil=new DatabaseUtil(this);

        //判断数据库中是否有用户信息，如果有则跳转至MainActivity没有跳转至LoginActivity
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user=databaseUtil.selectUserInfo();
                if(user!=null){
                    try {
                        Thread.sleep(1500);
                        Intent intent=new Intent(SplashActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    try {
                        Thread.sleep(1500);
                        Intent intent=new Intent(SplashActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        databaseUtil.closeDatabase();
    }
}
